package utgame.com.fakelegendary.activity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import java.util.Map;

import utgame.UtMessage.PlayerAction;
import utgame.UtMessage.MC_PlayerActionEatBall;
import utgame.UtMessage.MessagePacket.MessageType;
import utgame.UtMessage.MC_PlayerActionMove;
import utgame.com.fakelegendary.R;
import utgame.com.fakelegendary.container.HeroManager;
import utgame.com.fakelegendary.container.PlayerContainer;
import utgame.com.fakelegendary.model.Ball;
import utgame.com.fakelegendary.model.Hero;
import utgame.com.fakelegendary.socket.ServerSession;

/**
 * Created by xueal on 2018/1/23.
 */

public class GameView extends View {

    private static final int HeroRadius = 20;

    private final float step = 10;
//    private final float stepY = 10;

//    private float lastX = 50;
//    private float lastY = 50;

//    private float radius = 20;

    private Ball mBall = new Ball(100, 100, 10);

    boolean isEaten = false;

    private boolean mIsDrawHero = true;

    Paint mPaintOther = new Paint();
    Paint mPaintSelf = new Paint();


    public boolean getIsDrawHero() {
        return mIsDrawHero;
    }

    public void setIsDrawHero(boolean mIsDrawHero) {
        this.mIsDrawHero = mIsDrawHero;
    }

    public Ball getBall() {return mBall;}

    public GameView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaintOther.setColor(Color.RED);
        mPaintSelf.setColor(Color.BLUE);

//        canvas.drawCircle(lastX, lastY, HeroRadius, mPaintOther);

        String selfName = PlayerContainer.getInstance().getName();

        // 地图上其他英雄
        Map<String, Hero> heroMap = HeroManager.getInstance().getAllHero();
        for (Map.Entry<String, Hero> entry : heroMap.entrySet()) {
            Hero hero= entry.getValue();

            if (hero.getName().equals(selfName)){
//                isEaten = true;
                continue;
            }
            if (!hero.getBall().isValid()){
                continue;
            }
            canvas.drawCircle(hero.getBall().getX(), hero.getBall().getY(), hero.getBall().getR(), mPaintOther);
        }
        if (!isEaten){
            canvas.drawCircle(mBall.getX(), mBall.getY(), mBall.getR(), mPaintSelf);
        } else {
            Toast.makeText(getContext(), getResources().getString(R.string.lose), Toast.LENGTH_LONG).show();
            isEaten = false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int action = event.getAction();
        System.out.println(action);
        switch (action) {

            case MotionEvent.ACTION_DOWN:

                float stopX = event.getX();
                float stopY = event.getY();

                float lastX = mBall.getX();
                float lastY = mBall.getY();
                float radius = mBall.getR();

                float k = (stopY - lastY) / (stopX - lastX);

                float stepX = step / (float)(Math.sqrt(1 + k * k));
                float stepY = Math.abs(k) * step / (float)(1 + k * k);

                lastX = lastX + stepX * (stopX - lastX) / (Math.abs(stopX - lastX));
                lastY = lastY + stepX * (stopX - lastX) / (Math.abs(stopX - lastX)) * k;

                mBall.setX(lastX);
                mBall.setY(lastY);
                mBall.setR(radius);

                onBallMove();

                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
            default:
                break;
        }

        return super.onTouchEvent(event);
    }

    public void checkCollide(){

        String selfName = PlayerContainer.getInstance().getName();

        Map<String, Hero> heroMap = HeroManager.getInstance().getAllHero();
        for (Map.Entry<String, Hero> entry : heroMap.entrySet()) {
            Hero hero= entry.getValue();
            if (hero.getName().equals(selfName)){
                continue;
            }
            if (isCollide(mBall, hero.getBall())){
                if (mBall.getR() >= hero.getBall().getR()){
                    mBall.incR();

                    continue;
                } else {
                    HeroManager.getInstance().removeHero(selfName);
                    Toast.makeText(getContext(), getResources().getString(R.string.lose), Toast.LENGTH_LONG).show();
                }
            }
        }
    }

    public static boolean isCollide(Ball ball1, Ball ball2){
        return ((ball1.getX() - ball2.getX()) * (ball1.getX() - ball2.getX()) + (ball1.getY() - ball2.getY()) * (ball1.getY() - ball2.getY()))
                < ((ball1.getR() + ball2.getR()) * (ball1.getR() + ball2.getR()));
    }

    public void onBallMove(){

        // 发送数据到服务器
        MC_PlayerActionMove.Builder moveBuilder = MC_PlayerActionMove.newBuilder();

        PlayerAction.Builder playerActionBuilder = PlayerAction.newBuilder();
        playerActionBuilder.setName(PlayerContainer.getInstance().getName());
        playerActionBuilder.setX(mBall.getX());
        playerActionBuilder.setY(mBall.getY());
        playerActionBuilder.setR(mBall.getR());

        moveBuilder.setPlayerAction(playerActionBuilder);

        ServerSession.getInstance().sendMessage(MessageType.PlayerActionMove, moveBuilder.build().toByteString());

        checkCollide();

    }
}

